//
//  ICMProxyHomePageViewController.m
//  ICrEAM
//
//  Created by Cty on 2016/11/10.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "ICMProxyHomePageViewController.h"
#import "ICMHeaderView.h"
#import "ICMProgressHeaderView.h"
#import "ICMBottomBarView.h"
#import "ICMProxyBasicInfoView.h"
#import "MonitorViewController.h"
#import "ICMCustomerAndCarViewController.h"
#import "ICMCarModel.h"
#import "ICMPersonerModel.h"
#import "ICMProxyBasicInfoModel.h"
#import "ICMProxyMemberModel.h"
#import "ICMProxyActivityModel.h"
#import "ICMProxyActivity.h"
#import "ICMMemberView.h"
#import "ICMProxyOrderInfoView.h"
#import "ICMProxyMaintenanceAndPartsView.h"
#import "ICMProxyMemberSKDModel.h"
#import "RepairTypeSelectView.h"
#import "AppDelegate.h"
#import "ICMProxyPreviewViewController.h"
#import "ICMBasicInfoModel.h"
#import "ICMAppointmentOtherInfoModel.h"
#import "ICMPartsTopModel.h"
#import "ICMActivityModel.h"
#import "ICMAppointmentPreviewViewController.h"

#import "GetCarPreviewControllerViewController.h"
#import "BalancePresentController.h"
#import <AVFoundation/AVFoundation.h>
@interface ICMProxyHomePageViewController ()<ICMHeaderViewDelegate,ICMBottomBarViewDelegate,UIScrollViewDelegate,ICMProxyBasicInfoViewDelegate,ICMProxyOrderInfoViewDelegate,ICMProxyMaintenanceAndPartsViewDelegate,RepairTypeDelegate,ICMProgressHeaderViewDelegate,ICMProxyActivityDelegate,UIAlertViewDelegate>{
    BOOL _haveRecord;
    NSString *_file;
    NSString *LABOUR_PRICE;
}

@property (nonatomic, strong) UIScrollView *bigScrollView;//scrollview

@property (nonatomic, strong) ICMHeaderView *headerView;//头部view

@property (nonatomic, strong) UIView *blankView;//空白页

@property (nonatomic, strong) ICMProgressHeaderView *progressHeaderView;//单据进度view

@property (nonatomic, strong) ICMProxyBasicInfoView *proxyBasicInfoView;//车主/车辆信息
@property (nonatomic, strong) ICMMemberView *memberView;//会员信息

@property (nonatomic, strong) ICMProxyActivity *selecteActivityView;//活动view

@property (nonatomic, strong) ICMCarModel *carModel;//车辆model
@property (nonatomic, strong) ICMPersonerModel *personerModel;//车主model

@property (nonatomic, strong) ICMProxyOrderInfoView *proxyOrderInfoView;//单据信息

@property (nonatomic, strong) ICMProxyMaintenanceAndPartsView *proxyMaintenanceAndPartsView;//项目和零件

@property (nonatomic, strong) RepairTypeSelectView *repairTypeSelectView;//维修类型选择view

@property (nonatomic, weak) ICMBottomBarView *bottomBar;//底部bar

//根据活动里面项目和零件  和   项目和零件  生成对应关系数据字典
@property (nonatomic, strong) NSMutableDictionary *labourAndPartsExistDic;

//顶部进度条字典
@property (nonatomic, strong) NSMutableDictionary *progressDic;
@property(nonatomic,strong)AVAudioRecorder *audioRecorder;//录音
@property (nonatomic, assign) BOOL couldRecord;

//判断是否点过会员信息同步
@property (nonatomic, assign) BOOL isClickedMemberInfo;

//活动页显示信息用view
@property (nonatomic, strong) UIView *actMsgBgView;
@property (nonatomic, strong) UILabel *actMsgLabel;
@property (nonatomic, strong) UIView *actMsgBgView2;
@property (nonatomic, strong) UILabel *actMsgLabel2;

@end

@implementation ICMProxyHomePageViewController
-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    self.audioRecorder=nil;
    [self.audioRecorder stop];
}

- (void)addProgressHeaderView{
    [self.progressHeaderView removeFromSuperview];
    self.progressHeaderView = [ICMProgressHeaderView createProgressHeaderViewWithStateDic:self.progressDic andFrame:CGRectMake(0, ActualHeight(40), ScreenWidth, 66)];
    self.progressHeaderView.delegate = self;
    [self.view  insertSubview:self.progressHeaderView belowSubview:self.suspendView];
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title=@"委托书";
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.view.backgroundColor = BackgroundColor_White;
    self.lasttMileage=self.proxyBasicInfoModel.IN_MILEAGE;
//    self.selecteActivityView.activityModels = self.activityModels;
//    [self.selecteActivityView.activityTableView reloadData];
    
    self.headerView = [ICMHeaderView createHeaderViewWithFrame:CGRectMake(0, 0, ScreenWidth, ActualHeight(40)) andTitles:@[@"车主/车辆信息",@"单据信息",@"活动选择",@"项目和零件"] andType:@"0"];
    self.headerView.delegate = self;
    [self.view addSubview:self.headerView];
    
    self.progressDic = [@{@"0":self.proxyBasicInfoModel.BOOKING_ORDER_NO?:@"",@"1":self.proxyBasicInfoModel.RECEPTION_NO?:@"",@"2":self.proxyBasicInfoModel.RO_NO?:@""} mutableCopy];
    [self addProgressHeaderView];
    
    //配置页面信息
    [self setupBigScrollView];
    
    ICMBottomBarView *bottomBarView = [ICMBottomBarView createBottomBarViewWithTitles:@[@"录 音",@"预 览",@"保 存",@"打 印"] normalImages:@[[UIImage imageNamed:@"btn_record"],[UIImage imageNamed:@"btn_preview"],[UIImage imageNamed:@"btn_save"],[UIImage imageNamed:@"bottom_btn_print"]] seletedImages:nil andFrame:CGRectMake(0, ScreenHeight-70-NavBarAndStatusHeight, ScreenWidth, 70)];
    bottomBarView.delegate = self;
    
    [self.view addSubview:bottomBarView];
    self.bottomBar = bottomBarView;
    if (self.proxyBasicInfoModel.VIN) {
        [self MonitorViewController];
    }
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(presentVC:) name:@"popoverSelectionView" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didSelectRepairType:) name:@"RepairTypeChanged" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proxyActivityLabourChanged:) name:@"proxyActivityLabourChanged" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proxyActivityPartChanged:) name:@"ProxyActivityPartChanged" object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proxyActivityLabourChanged2:) name:@"proxyActivityLabourChanged2" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proxyActivityPartChanged2:) name:@"ProxyActivityPartChanged2" object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clickActivityTypeInProxy:) name:@"ClickActivityTypeInProxy" object:nil];
    //各页面值变化通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(viewValueChanged:) name:@"ViewValueChanged" object:nil];
    
    //头部跳转用赋值
    super.super_VIN = self.proxyBasicInfoModel.VIN;
    super.super_owner_no = self.proxyBasicInfoModel.OWNER_NO;
    
    //底部button逻辑控制
    // APP预生成，则预览按钮、打印按钮不能点击
    if([self.proxyBasicInfoModel.RO_STATUS isEqualToString:ICMEnumIntToString(ICMEnumProxyTypeFromAPP)])
    {
        UIButton *button1 = self.bottomBar.bottomButtons[1];
        button1.alpha = 0.5;
        button1.enabled = NO;
        
        UIButton *button3 = self.bottomBar.bottomButtons[3];
        button3.alpha = 0.5;
        button3.enabled = NO;
        
        [self clickMemberBtn];//app预生成状态强制同步大众一家
    }
    else
    {
        UIButton *button1 = self.bottomBar.bottomButtons[1];
        button1.alpha = 1;
        button1.enabled = YES;
        
        UIButton *button3 = self.bottomBar.bottomButtons[3];
        button3.alpha = 1;
        button3.enabled = YES;
    }
    
}
-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    


}
#pragma mark ICMProgressHeaderView点击回调方法
/*ICMProgressHeaderViewDelegate*/
- (void)progressHeaderViewDidSelectedIndex:(NSInteger)index{
    if(index == 0)
    {
        //预约预览
        if(self.proxyBasicInfoModel.BOOKING_ORDER_NO && ![self.proxyBasicInfoModel.BOOKING_ORDER_NO isEqualToString:@""])
        {
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters setObject:self.proxyBasicInfoModel.BOOKING_ORDER_NO forKey:@"ORDER_ID"];
            ICMRequestManager *manager = [[ICMRequestManager alloc]init];
            [manager startGetRequestWithURL:BookingOrderDetils parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
                if ([data[@"CODE"] integerValue] == 200) {
                    if (IsArr(data[@"TT_BOOKING_OEDER"])) {
                        ICMAppointmentBasicInfoModel *tempBasicInfoModel = [ICMAppointmentBasicInfoModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
                        ICMAppointmentOtherInfoModel *tempBasicOtherModel = [ICMAppointmentOtherInfoModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
                        ICMPartsTopModel *tempPartsTopModel = [ICMPartsTopModel yy_modelWithDictionary:data[@"TT_BOOKING_OEDER"][0]];
                        
                        ICMRepairMaintenancesModel *tempMaintenancesModel = [ICMRepairMaintenancesModel yy_modelWithDictionary:data];
                        [tempMaintenancesModel createRepairMaintenanceBookingModel];
                        
                        ICMRepairPartsArrayModel *tempPartsArrayModel = [ICMRepairPartsArrayModel yy_modelWithDictionary:data];
                        [tempPartsArrayModel createRepairPartsBookingModel];
                        
                        ICMActivityModel *tempActivityModel = [ICMActivityModel yy_modelWithDictionary:data];
                        [tempActivityModel createActivityItemBookingModel];
                        
                        ICMAppointmentPreviewViewController *PVC = [[ICMAppointmentPreviewViewController alloc]init];
                        PVC.basicInfoModel = tempBasicInfoModel;
                        PVC.basicOtherModel = tempBasicOtherModel;
                        PVC.partsTopModel = tempPartsTopModel;
                        PVC.maintenancesModel = tempMaintenancesModel;
                        PVC.partsArrayModel = tempPartsArrayModel;
                        PVC.activityModel = tempActivityModel;
                        [self.navigationController pushViewController:PVC animated:YES];
                    }
                    
                }
            }];
        }
    }
    if(index == 1)
    {
        //接车预览
        if(self.proxyBasicInfoModel.RECEPTION_NO && ![self.proxyBasicInfoModel.RECEPTION_NO isEqualToString:@""])
        {
            NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
            [parameters setObject:self.proxyBasicInfoModel.RECEPTION_NO forKey:@"RECEPTION_NO"];
            ICMRequestManager *manager = [[ICMRequestManager alloc]init];
            [manager startGetRequestWithURL:ReceptionVehicleOrderDetils parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
                if ([data[@"CODE"] integerValue] == 200) {
                    NSMutableArray *imageModels = [NSMutableArray array];
                    if (IsArr(data[@"TT_RECPTION_DAMANGE"])) {
                        NSArray *images = data[@"TT_RECPTION_DAMANGE"];
                        for (NSDictionary *dic in images) {
                            ICMPictureModel *model = [ICMPictureModel yy_modelWithDictionary:dic];
                            [imageModels addObject:model];
                        }
                    }
                    
                    GetCarPreviewControllerViewController *GCP = [GetCarPreviewControllerViewController new];
                    
                    if (IsArr(data[@"TT_RECEPTION_VEHICLE_ORDER"])) {
                        ICMBasicInfoModel *infoModel = [ICMBasicInfoModel yy_modelWithDictionary:data[@"TT_RECEPTION_VEHICLE_ORDER"][0]];
                        GCP.basicInfoModel = infoModel;
                    }
                    if (IsArr(data[@"TT_RECEPTION_VEHICLE_ORDER"])) {
                        ICMCarePackageDownModel *carePackageDownModel = [ICMCarePackageDownModel yy_modelWithDictionary:data[@"TT_RECEPTION_VEHICLE_ORDER"][0]];
                        GCP.carePackageDownModel = carePackageDownModel;
                    }
                    if (IsArr(data[@"TT_RECEPTION_VEHICLE_ORDER"])) {
                        ICMCheckAppearanceDownModel *checkAppearanceDownModel = [ICMCheckAppearanceDownModel yy_modelWithDictionary:data[@"TT_RECEPTION_VEHICLE_ORDER"][0]];
                        GCP.checkAppearanceDownModel = checkAppearanceDownModel;
                    }
                    if (IsArr(data[@"TT_RECEPTION_VEHICLE_ORDER"])) {
                        ICMPartsTopModel *partsTopModel = [ICMPartsTopModel yy_modelWithDictionary:data[@"TT_RECEPTION_VEHICLE_ORDER"][0]];
                        GCP.partsTopModel = partsTopModel;
                    }
                    
                    ICMCarePackageModel *carePackageModel = [ICMCarePackageModel yy_modelWithDictionary:data];
                    [carePackageModel createCarePackageItemModel];
                    GCP.carePackageModel = carePackageModel;
                    
                    ICMRepairMaintenancesModel *repairMaintenanceModel = [ICMRepairMaintenancesModel yy_modelWithDictionary:data];
                    [repairMaintenanceModel createRepairMaintenanceModel];
                    
                    ICMRepairPartsArrayModel *repairPartsModel = [ICMRepairPartsArrayModel yy_modelWithDictionary:data];
                    [repairPartsModel createRepairPartsModel];
                    
                    ICMActivityModel *activityModel = [ICMActivityModel yy_modelWithDictionary:data];
                    [activityModel createActivityItemModel];
                    
                    ICMCheckAppearanceTopModel *checkAppearanceTopModel = [ICMCheckAppearanceTopModel yy_modelWithDictionary:data];
                    [checkAppearanceTopModel createPictureModel];
                    
                    GCP.repairEventModel = repairMaintenanceModel;
                    GCP.repairPartsModel = repairPartsModel;
                    GCP.activityModel = activityModel;
                    GCP.checkAppearanceTopModel = checkAppearanceTopModel;
                    [self.navigationController pushViewController:GCP animated:YES];        }
            }];
        }
    }
    
}

#pragma mark 点击活动相应通知回调方法
- (void)clickActivityTypeInProxy:(NSNotification *)notification
{
    [self.bigScrollView setContentOffset:CGPointMake(2*ScreenWidth, 0) animated:YES];
}

#pragma mark 活动页项目选择变化通知回调方法
- (void)proxyActivityLabourChanged:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSString *actCode = dic[@"Activity_Code"];
    NSString *labourCode = dic[@"LabourCode"];
    NSNumber *isSelected = dic[@"IsSelected"];
    
    NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
    NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANLabour"];
    if([dic2 objectForKey:labourCode])
        [dic2 setObject:isSelected forKey:labourCode];
    self.selecteActivityView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.proxyMaintenanceAndPartsView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    [self.selecteActivityView.activityTableView reloadData];
    
    for(int i = 0;i<self.activityTempModels.count;i++)
    {
        ICMProxyActivityViewModel *actModel = self.activityTempModels[i];
        if([actModel.subTitle isEqualToString:actCode])
        {
            NSMutableArray *labourArray = actModel.allActivityModel.TT_ACTIVITY_LABOUR;
            for(int j = 0;j<labourArray.count;j++)
            {
                ICMRepairMaintenanceModel *labourModel = labourArray[j];
                if([labourModel.LABOUR_CODE isEqualToString:labourCode])
                {
                    [self.proxyMaintenanceAndPartsView proxyActivityLabourChangedWithModel:labourModel IsSelected:[isSelected boolValue]];
                    [self.proxyOrderInfoView resetRepairTypes:self.selectedRepairTypes];
                    return;
                }
            }
        }
    }
    
    
    
}

#pragma mark 活动页零件选择变化通知回调方法
- (void)proxyActivityPartChanged:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSString *actCode = dic[@"Activity_Code"];
    NSString *partCode = dic[@"PartCode"];
    NSNumber *isSelected = dic[@"IsSelected"];
    
    NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
    NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANParts"];
    if([dic2 objectForKey:partCode])
        [dic2 setObject:isSelected forKey:partCode];
    
    self.selecteActivityView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.proxyMaintenanceAndPartsView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    
    [self.selecteActivityView.activityTableView reloadData];
    
    for(int i = 0;i<self.activityTempModels.count;i++)
    {
        ICMProxyActivityViewModel *actModel = self.activityTempModels[i];
        if([actModel.subTitle isEqualToString:actCode])
        {
            NSMutableArray *partsArray = actModel.allActivityModel.TT_ACTIVITY_PART;
            for(int j = 0;j<partsArray.count;j++)
            {
                ICMRepairPartsModel *partModel = partsArray[j];
                if([partModel.PART_NO isEqualToString:partCode])
                {
                    [self.proxyMaintenanceAndPartsView proxyActivityPartChanged:partModel IsSelected:[isSelected boolValue]];
                    [self.proxyOrderInfoView resetRepairTypes:self.selectedRepairTypes];
                    return;
                }
            }
        }
    }
}

#pragma mark 项目零件页活动的项目变化通知回调方法
- (void)proxyActivityLabourChanged2:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSString *actCode = dic[@"Activity_Code"];
    NSString *labourCode = dic[@"LabourCode"];
    NSNumber *isSelected = dic[@"IsSelected"];
    
    NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
    NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANLabour"];
    if([dic2 objectForKey:labourCode])
        [dic2 setObject:isSelected forKey:labourCode];
    self.selecteActivityView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.proxyMaintenanceAndPartsView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    [self.selecteActivityView.activityTableView reloadData];
    
    
    
    
}

#pragma mark 项目零件页活动的零件变化通知回调方法
- (void)proxyActivityPartChanged2:(NSNotification *)notification
{
    NSDictionary *dic = notification.userInfo;
    NSString *actCode = dic[@"Activity_Code"];
    NSString *partCode = dic[@"PartCode"];
    NSNumber *isSelected = dic[@"IsSelected"];
    
    NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
    NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANParts"];
    if([dic2 objectForKey:partCode])
        [dic2 setObject:isSelected forKey:partCode];
    
    self.selecteActivityView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.proxyMaintenanceAndPartsView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    
    [self.selecteActivityView.activityTableView reloadData];
    
}

#pragma mark 设置页面布局
- (void)setupBigScrollView{
    
    //配置页面信息时先清空view
    if(self.bigScrollView)
    {
        [self.bigScrollView removeFromSuperview];
        self.bigScrollView = nil;
    }
    if(self.proxyBasicInfoView)
    {
        [self.proxyBasicInfoView removeFromSuperview];
        self.proxyBasicInfoView = nil;
    }
    if(self.proxyOrderInfoView)
    {
        [self.proxyOrderInfoView removeFromSuperview];
        self.proxyOrderInfoView = nil;
    }
    if(self.selecteActivityView)
    {
        [self.selecteActivityView removeFromSuperview];
        self.selecteActivityView = nil;
    }
    if(self.proxyMaintenanceAndPartsView)
    {
        [self.proxyMaintenanceAndPartsView removeFromSuperview];
        self.proxyMaintenanceAndPartsView = nil;
    }
    
    //根据活动里面项目和零件  和   项目和零件  生成对应关系数据字典
    [self createLabourAndPartsExistDic];
    
    self.bigScrollView = [[UIScrollView alloc]initWithFrame:CGRectMake( 0, self.progressHeaderView.bottomEdge, ScreenWidth, ScreenHeight-self.headerView.frame.size.height-self.progressHeaderView.frame.size.height-70-NavBarAndStatusHeight)];
    
    self.bigScrollView.contentSize = CGSizeMake(ScreenWidth*4, ScreenHeight-self.headerView.frame.size.height-self.progressHeaderView.frame.size.height-70-NavBarAndStatusHeight);
    
    self.proxyBasicInfoView = [ICMProxyBasicInfoView createICMProxyBasicInfoViewWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.proxyBasicInfoView.delegate = self;
    [self.proxyBasicInfoView setUpWithModel:self.proxyBasicInfoModel];
    [self.bigScrollView addSubview:self.proxyBasicInfoView];
    
    self.proxyOrderInfoView = [ICMProxyOrderInfoView createICMOrderInfoViewWithFrame:CGRectMake(ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.proxyOrderInfoView.delegate = self;
    [self.proxyOrderInfoView setupOrderInfoViewWithBasicInfoModel:self.proxyBasicInfoModel];
    [self.bigScrollView addSubview:self.proxyOrderInfoView];
    
    self.selecteActivityView = [ICMProxyActivity createSelecteActivityViewWithFrame:CGRectMake(2*ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.selecteActivityView.delegate = self;
    self.selecteActivityView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.selecteActivityView.activityModels = self.activityModels;
    [self.selecteActivityView resetSelectedActivity];
    [self.bigScrollView addSubview:self.selecteActivityView];
    
    self.proxyMaintenanceAndPartsView = [ICMProxyMaintenanceAndPartsView createICMMaintenanceAndPartsViewWithFrame:CGRectMake(3*ScreenWidth, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
    self.proxyMaintenanceAndPartsView.delegate = self;
    self.proxyMaintenanceAndPartsView.labourAndPartsExistDic = self.labourAndPartsExistDic;
    self.proxyMaintenanceAndPartsView.modelCode = self.proxyBasicInfoModel.MODEL_CODE;
    [self.proxyMaintenanceAndPartsView setupPartsViewWithBasicModel:self.proxyBasicInfoModel];;
    [self.proxyMaintenanceAndPartsView setupPartsViewWithRepairMaintenancesModel:self.repairMaintenancesModel];
    [self.proxyMaintenanceAndPartsView setupPartsViewWithRepairPartsArrayModel:self.repairPartsArrayModel];
    
    self.selectedRepairTypes = [[self.proxyBasicInfoModel.RO_REPAIR_TYPE_NAME componentsSeparatedByString:@";"]mutableCopy];
    
    self.selecteActivityView.repairTypes = self.selectedRepairTypes;
    self.proxyMaintenanceAndPartsView.repairTypes = self.selecteActivityView.repairTypes;
    
    self.proxyMaintenanceAndPartsView.repairMaintenancesModel = self.repairMaintenancesModel;
    self.proxyMaintenanceAndPartsView.stationModel = self.stationModel;
    [self getLABOUR_PRICE:self.proxyBasicInfoModel.MODEL_CODE];
    
    [self.proxyMaintenanceAndPartsView resetRepairTypeButton];
    [self.bigScrollView addSubview:self.proxyMaintenanceAndPartsView];
    
    self.bigScrollView.pagingEnabled = YES;
    self.bigScrollView.delegate = self;
    self.bigScrollView.showsHorizontalScrollIndicator = NO;
    //[self.view addSubview:self.bigScrollView];
    [self.view insertSubview:self.bigScrollView belowSubview:self.suspendView];
    
    //判断是否有 质量担保保养 如果存在 活动页面不可点击
    BOOL hasServiceActivity = NO;
    for (int i = 0; i < self.selectedRepairTypes.count; i++) {
        NSString *temp = self.selectedRepairTypes[i];
        if([temp isEqualToString:@"质量担保保养"])
        {
            hasServiceActivity = YES;
        }
    }
    if(hasServiceActivity)
    {
        //存在 质量担保保养
        if(self.actMsgBgView)
        {
            [self.actMsgBgView removeFromSuperview];
        }
        [self.selecteActivityView addSubview:self.actMsgBgView];
        if(self.actMsgLabel)
        {
            [self.actMsgLabel removeFromSuperview];
        }
        [self.actMsgBgView addSubview:self.actMsgLabel];
    }
    else
    {
        //不存在 质量担保保养
        [self.actMsgBgView removeFromSuperview];
        [self.actMsgLabel removeFromSuperview];
        self.actMsgBgView = nil;
        self.actMsgLabel = nil;
    }
    
    
}

- (void)haveChangeRepairTypes{
    [self.proxyMaintenanceAndPartsView resetRepairTypeButton];
}

-(NSMutableArray *)activityTempModels{
    if (!_activityTempModels) {
        _activityTempModels = [NSMutableArray array];
    }
    return _activityTempModels;
}

#pragma mark 根据活动里面项目和零件  和   项目和零件  生成对应关系数据字典
- (void)createLabourAndPartsExistDic
{
    [self.labourAndPartsExistDic removeAllObjects];
    for(int i = 0;i<self.activityTempModels.count;i++)
    {
        ICMProxyActivityViewModel *actModel = self.activityTempModels[i];
        NSString *actCode = actModel.subTitle;
        NSMutableDictionary *actDic = [[NSMutableDictionary alloc] init];
        //该活动下项目的数组
        NSMutableArray *labourArray = actModel.allActivityModel.TT_ACTIVITY_LABOUR;
        //该活动下项目的字典
        NSMutableDictionary *labourDic = [[NSMutableDictionary alloc] init];
        for(int j = 0;j<labourArray.count;j++)
        {
            ICMRepairMaintenanceModel *labourModel = labourArray[j];
            NSString *labourCode = labourModel.LABOUR_CODE;
            [labourDic setObject:@(NO) forKey:labourCode];
        }
        //该活动下零件的数组
        NSMutableArray *partsArray = actModel.allActivityModel.TT_ACTIVITY_PART;
        //该活动下零件的字典
        NSMutableDictionary *partsDic = [[NSMutableDictionary alloc] init];
        for(int j = 0;j<partsArray.count;j++)
        {
            ICMRepairPartsModel *partModel = partsArray[j];
            NSString *partCode = partModel.PART_NO;
            [partsDic setObject:@(NO) forKey:partCode];
        }
        [actDic setObject:labourDic forKey:@"PANLabour"];
        [actDic setObject:partsDic forKey:@"PANParts"];
        [self.labourAndPartsExistDic setObject:actDic forKey:actCode];
        
        //项目数组
        NSMutableArray *proxyLabourArray = self.repairMaintenancesModel.TT_RO_LABOUR;
        for(int z = 0;z<proxyLabourArray.count;z++)
        {
            ICMRepairMaintenanceModel *model = proxyLabourArray[z];
            if([model.ACTIVITY_NO isEqualToString:actCode])
            {
                NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
                NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANLabour"];
                if([dic2 objectForKey:model.LABOUR_CODE])
                    [dic2 setObject:@(YES) forKey:model.LABOUR_CODE];
            }
        }
        
        //∫零件数组
        NSMutableArray *proxyPartsArray = self.repairPartsArrayModel.TT_RO_REPAIR_PART;
        for(int z = 0;z<proxyPartsArray.count;z++)
        {
            ICMRepairPartsModel *model = proxyPartsArray[z];
            if([model.ACTIVITY_NO isEqualToString:actCode])
            {
                NSMutableDictionary *dic1 = [self.labourAndPartsExistDic objectForKey:actCode];
                NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANParts"];
                if([dic2 objectForKey:model.PART_NO])
                    [dic2 setObject:@(YES) forKey:model.PART_NO];
            }
        }
    }
}

#pragma mark 根据labourAndPartsExistDic 返回被选择的活动的被选择的项目和零件 预览用
- (NSMutableArray *)previewActivitysWithDic:(NSMutableDictionary *)dic
{
    NSMutableArray *temp = [[NSMutableArray alloc] init];
    NSMutableArray *previewActivityArray = [[NSMutableArray alloc] initWithArray:self.activityTempModels];
    for(int z=0;z< previewActivityArray.count;z++)
    {
        ICMProxyActivityViewModel *actModel = previewActivityArray[z];
        if([actModel.IS_CHECKED isEqualToString:ICMEnumIntToString(ICMEnumYESOrNOYES)])
        {
            NSString *actCode = actModel.subTitle;
            NSMutableDictionary *dic1 = [dic objectForKey:actCode];
            NSMutableDictionary *dic2 = [dic1 objectForKey:@"PANLabour"];
            NSMutableDictionary *dic3 = [dic1 objectForKey:@"PANParts"];
            
            //该活动下项目的数组
            NSMutableArray *tempLabourArray = [[NSMutableArray alloc] init];
            NSMutableArray *labourArray = [[NSMutableArray alloc] initWithArray:actModel.allActivityModel.TT_ACTIVITY_LABOUR];
            //该活动下项目的字典
            for(int i=0;i<labourArray.count;i++)
            {
                ICMRepairMaintenanceModel *labourModel = labourArray[i];
                NSString *labourCode = labourModel.LABOUR_CODE;
                if([dic2 objectForKey:labourCode])
                {
                    if(![[dic2 objectForKey:labourCode] boolValue])
                    {
                        [tempLabourArray addObject:labourModel];
                    }
                }
            }
            for(int ii = 0;ii<tempLabourArray.count;ii++)
            {
                [labourArray removeObject:tempLabourArray[ii]];
            }
            
            actModel.allActivityModel.TT_ACTIVITY_LABOUR = [[NSMutableArray alloc] initWithArray:labourArray];
            //该活动下零件的数组
            NSMutableArray *tempPartsArray = [[NSMutableArray alloc] init];
            NSMutableArray *partsArray = [[NSMutableArray alloc] initWithArray:actModel.allActivityModel.TT_ACTIVITY_PART];
            //该活动下零件的字典
            for(int j=0;j<partsArray.count;j++)
            {
                ICMRepairPartsModel *partModel = partsArray[j];
                NSString *partCode = partModel.PART_NO;
                if([dic3 objectForKey:partCode])
                {
                    if(![[dic3 objectForKey:partCode] boolValue])
                    {
                        [tempPartsArray addObject:partModel];
                        //[partsArray removeObject:partModel];
                    }
                }
            }
            for(int jj = 0;jj<tempPartsArray.count;jj++)
            {
                [partsArray removeObject:tempPartsArray[jj]];
            }
            actModel.allActivityModel.TT_ACTIVITY_PART = [[NSMutableArray alloc] initWithArray:partsArray];
        }
        else
        {
            [temp addObject:actModel];
            //[previewActivityArray removeObject:actModel];
        }
    }
    for(int zz = 0;zz<temp.count;zz++)
    {
        [previewActivityArray removeObject:temp[zz]];
    }
    
    return previewActivityArray;
}

- (NSMutableDictionary *)labourAndPartsExistDic
{
    if(!_labourAndPartsExistDic)
    {
        _labourAndPartsExistDic = [[NSMutableDictionary alloc] init];
    }
    return _labourAndPartsExistDic;
}

- (UIView *)blankView{
    if (!_blankView) {
        _blankView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _blankView.backgroundColor = [UIColor colorWithHex:BorderColor_Gray_Hex andAlpha:0.8];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideBlankView)];
        [_blankView addGestureRecognizer:tap];
    }
    return _blankView;
}

#pragma mark 隐藏blankview
- (void)hideBlankView{
    [self.blankView removeFromSuperview];
    if (self.memberView) {
        [self.memberView removeFromSuperview];
    }
    for (UIView *view in self.blankView.subviews) {
        [view removeFromSuperview];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/**********************UIScrollViewDelegate***************************/
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float offsetX = scrollView.contentOffset.x;
    self.headerView.selectedIndex = offsetX/ScreenWidth;
    
    if(scrollView.contentOffset.x == 2*ScreenWidth)
    {
        //判断是否有 质量担保保养 如果存在 活动页面不可点击
        BOOL hasServiceActivity = NO;
        for (int i = 0; i < self.selectedRepairTypes.count; i++) {
            NSString *temp = self.selectedRepairTypes[i];
            if([temp isEqualToString:@"质量担保保养"])
            {
                hasServiceActivity = YES;
            }
        }
        if(hasServiceActivity)
        {
            //存在 质量担保保养

        }
        else
        {
            //不存在 质量担保保养
            if(!self.activityModels || self.activityModels.count == 0)
            {
                if(self.actMsgBgView2)
                {
                    [self.actMsgBgView2 removeFromSuperview];
                }
                [self.selecteActivityView addSubview:self.actMsgBgView2];
                if(self.actMsgLabel2)
                {
                    [self.actMsgLabel2 removeFromSuperview];
                }
                [self.actMsgBgView2 addSubview:self.actMsgLabel2];
            }
        }
        
    }
    else
    {
        //[self.bigScrollView addSubview:self.selecteActivityView];
        [self.actMsgBgView2 removeFromSuperview];
        [self.actMsgLabel2 removeFromSuperview];
        self.actMsgBgView2 = nil;
        self.actMsgLabel2 = nil;
    }
}

#pragma mark 活动页显示信息用主view
- (UIView *)actMsgBgView
{
    if(!_actMsgBgView)
    {
        _actMsgBgView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
        _actMsgBgView.backgroundColor = BackgroundColor_Gray;//[UIColor colorWithWhite:0.5 alpha:0.5];
    }
    return _actMsgBgView;
}

#pragma mark 活动页显示信息用label
- (UILabel *)actMsgLabel
{
    if(!_actMsgLabel)
    {
        _actMsgLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, self.actMsgBgView.frame.size.height/2-25, ScreenWidth, 50)];
        _actMsgLabel.text = @"维修类型为质量担保保养，服务活动不可选！";
        _actMsgLabel.textAlignment = NSTextAlignmentCenter;
        //_actMsgLabel.backgroundColor = TitleColor_Dark;
        _actMsgLabel.textColor = [UIColor blackColor];
        [_actMsgLabel sizeToFit];
        CGRect rect = _actMsgLabel.frame;
        rect.origin.x = (ScreenWidth - rect.size.width)/2;
        rect.origin.y = (self.actMsgBgView.frame.size.height - rect.size.height)/2;
        _actMsgLabel.frame = rect;
    }
    return _actMsgLabel;
}

#pragma mark 活动页显示信息用主view2
- (UIView *)actMsgBgView2
{
    if(!_actMsgBgView2)
    {
        _actMsgBgView2 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, CGRectGetHeight(self.bigScrollView.frame))];
        _actMsgBgView2.backgroundColor = BackgroundColor_Gray;//[UIColor colorWithWhite:0.5 alpha:0.5];
    }
    return _actMsgBgView2;
}

#pragma mark 活动页显示信息用label
- (UILabel *)actMsgLabel2
{
    if(!_actMsgLabel2)
    {
        _actMsgLabel2 = [[UILabel alloc] initWithFrame:CGRectMake(0, self.actMsgBgView2.frame.size.height/2-25, ScreenWidth, 50)];
        _actMsgLabel2.text = @"该车型暂无可参加的活动！";
        _actMsgLabel2.textAlignment = NSTextAlignmentCenter;
        //_actMsgLabel.backgroundColor = TitleColor_Dark;
        _actMsgLabel2.textColor = [UIColor blackColor];
        [_actMsgLabel2 sizeToFit];
        CGRect rect = _actMsgLabel2.frame;
        rect.origin.x = (ScreenWidth - rect.size.width)/2;
        rect.origin.y = (self.actMsgBgView2.frame.size.height - rect.size.height)/2;
        _actMsgLabel2.frame = rect;
    }
    return _actMsgLabel2;
}

#pragma mark headview点击title回调方法
/**********************ICMHeaderViewDelegate***************************/
- (void)headerViewDidSelectedButtonAtIndex:(NSInteger)index{
    [self.bigScrollView scrollRectToVisible:CGRectMake(ScreenWidth*index, 0, ScreenWidth, ScreenHeight-240) animated:YES];
    if(index == 3){
        [self getLABOUR_PRICE:self.proxyBasicInfoModel.MODEL_CODE];
        
    }

}

#pragma mark -- 上传录音接口
- (void)uploadMusic{
    
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    [manager startPostMusicWithURL:nil parameters:nil musicUrl:[NSURL fileURLWithPath:micTempPath] inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            //保存
            [self.view showString:@"录音上传成功" forSeconds:1.5];
            _file = data[@"FILE"]?:@"";
            self.bottomBar.recorderLabel.text = @"录 音";
            _haveRecord = NO;
            [self startToSaveProxyInfoToServer];
        }else{
            [self.view showString:@"录音上传失败" forSeconds:1.5];
            [self startToSaveProxyInfoToServer];
        }
        if (failText) {
            [self.view showString:@"录音上传失败" forSeconds:1.5];
            //结算
           [self startToSaveProxyInfoToServer];
        }
    }];
}

#pragma mark bottomview点击botton回调方法
/**********************ICMBottomBarViewDelegate***************************/
- (void)bottomBarViewDidSelectIndex:(NSInteger)index{
    if (index == 0) {
        if ([self.bottomBar.recorderLabel.text isEqualToString:@"录 音"]) {
            self.bottomBar.recorderLabel.text = @"暂 停";
            [self.view showString:@"录音开始" forSeconds:1.5];
            [self.audioRecorder record];
            _haveRecord = YES;
        }else{
            self.bottomBar.recorderLabel.text = @"录 音";
            [self.view showString:@"录音暂停" forSeconds:1.5];
            [self.audioRecorder pause];
        }
        
    }
    if (index == 1) {
        //预览
        ICMProxyPreviewViewController *PVC = [[ICMProxyPreviewViewController alloc]init];
        PVC.activityModels = [self previewActivitysWithDic:self.labourAndPartsExistDic];
        PVC.repairMaintenancesModel = self.repairMaintenancesModel;
        PVC.proxyBasicInfoModel = self.proxyBasicInfoModel;
        PVC.repairPartsArrayModel = self.repairPartsArrayModel;
        PVC.stationsModel = _stationModel;
        
        [self.navigationController pushViewController:PVC animated:YES];
    }
    if(index == 2)
    {
        //保存
        if (_haveRecord) {
            [self clickStopBtn];
            [self.view showString:@"录音结束" forSeconds:1.5];
        }else{
            [self startToSaveProxyInfoToServer];
        }
        
    }
    if(index == 3)
    {
        BalancePresentController *presentCtr=[[BalancePresentController alloc]init];
        presentCtr.delegate=self;
        presentCtr.modalPresentationStyle = UIModalPresentationFormSheet;
        presentCtr.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
        presentCtr.preferredContentSize=CGSizeMake(400, 300);;
        presentCtr.printType=ProxyPrintType;
        presentCtr.proxy_No=self.proxyBasicInfoModel.RO_NO;
        presentCtr.extimatePrice=[NSString stringWithFormat:@"%.2f",[self.proxyBasicInfoModel.REAL_TOTAL_AMOUNT doubleValue]];
        [self presentViewController:presentCtr animated:YES completion:nil];
        //打印
   
    }
}
-(void)printProxyWithType:(PrintType) printType roNum:(NSString *)proxy_No extimatePrice:(NSString *)extimatePrice{
    [ICrEAMTool ipadPrintWithPrintType:printType printId:proxy_No Controller:self andEstimatePrice:extimatePrice];
}
#pragma mark 上传model转dic
- (NSMutableDictionary *)setupSaveData
{
    //    [self getAllDataModels];
    
    NSMutableDictionary *dataDic = [NSMutableDictionary dictionary];
    self.proxyBasicInfoModel.MP3_FILE = _file;
    NSDictionary *proxyBasicInfoModelDic = [self.proxyBasicInfoModel yy_modelToJSONObject];
    NSDictionary *repairmaintenancesModelDic = [self.repairMaintenancesModel yy_modelToJSONObject];
    NSDictionary *repairPartsModelDic = [self.repairPartsArrayModel yy_modelToJSONObject];
    
    //上传选中的活动
    NSMutableString *selActArray = [[NSMutableString alloc] initWithString:@""];
    for(int i = 0;i<self.selectedActivityModels.count;i++)
    {
        ICMProxyActivityViewModel *actModel = self.selectedActivityModels[i];
        if(actModel && actModel.subTitle && ![actModel.subTitle isEqualToString:@""])
        {
            [selActArray appendString:actModel.subTitle];
            if(i < self.selectedActivityModels.count-1)
            {
                [selActArray appendString:@";"];
            }
        }
    }
    [proxyBasicInfoModelDic setValue:selActArray forKey:@"JOIN_ACTIVITY_CODE"];
    
    NSArray *proxyBasicInfoArray = [[NSArray alloc] initWithObjects:proxyBasicInfoModelDic, nil];
    
    [dataDic setObject:proxyBasicInfoArray forKey:@"TT_REPAIR_ORDER"];
    [dataDic addEntriesFromDictionary:repairmaintenancesModelDic];
    [dataDic addEntriesFromDictionary:repairPartsModelDic];
    //[dataDic setObject:[self.activityModels yy_modelToJSONObject] forKey:@"TT_ACTIVITY"];
    

    return dataDic;
}

#pragma mark 从view获取model
- (void)getAllDataModels{
   
    [self.proxyBasicInfoModel setupBasicInfoModelWithBasicInfoView:self.proxyBasicInfoView WithProxyOrderInfoView:self.proxyOrderInfoView WithICMProxyMaintenanceAndPartsView:self.proxyMaintenanceAndPartsView];
    //self.activityModels = self.selecteActivityView.selectedActivitys;
    self.selectedActivityModels = self.selecteActivityView.selectedActivitys;
    [self.repairMaintenancesModel setupRepairMaintenancesModelWithRoPartsView:self.proxyMaintenanceAndPartsView];
    [self.repairPartsArrayModel setupRepairPartsArrayModelWithRoPartsView:self.proxyMaintenanceAndPartsView];
    
}


#pragma mark 保存委托书
- (void)startToSaveProxyInfoToServer{
    [self getAllDataModels];
    //检查数据为空或者有误
    if (![self checkData]) {
        return;
    }
    //如果维修类型为质量担保保养，则有必填字段
    if (![self checkRepairType])
    {
        return;
    }
    //验证维修类型里面是否有项目
    if(![self didExistsLabours])
    {
        return;
    }
    
    NSString *url = [NSString stringWithFormat:@"/INFO_ICREAM_WEB/App/saveRepairOrder"];
    [[[ICMRequestManager alloc]init]startPostRequestWithURL:url parameters:[self setupSaveData] inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            //保存成功
            self.hasViewValueChanged = NO;
            [self.view showString:@"保存成功!" forSeconds:1.5f];
            [ICrEAMTool savaOperationReceiptUserName:self.proxyBasicInfoModel.DELIVERER carSeries:self.proxyBasicInfoModel.SERIES_NAME licence:self.proxyBasicInfoModel.LICENSE status:@"委托书新建" returnTime:StrIsZero_gdm(self.proxyBasicInfoModel.END_TIME_SUPPOSED)?@"":self.proxyBasicInfoModel.END_TIME_SUPPOSED];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"DidSavedProxy" object:self];
            //[self.navigationController popViewControllerAnimated:YES];
            UIButton *button1 = self.bottomBar.bottomButtons[1];
            button1.alpha = 1;
            button1.enabled = YES;
            
            UIButton *button3 = self.bottomBar.bottomButtons[3];
            button3.alpha = 1;
            button3.enabled = YES;
            //保存成功之后重新设置view
            [self resetViewsAfterSave];
        }else{
            //保存失败
            if([data[@"CODE"] integerValue] == -200)
            {
                [self.view showString:data[@"MSG"] forSeconds:1.5f];
            }
            else
            {
                [self.view showString:@"保存失败!" forSeconds:1.5f];
            }
            
        }
    }];
}



#pragma mark 检查数据为空或者有误
- (BOOL)checkData {
    //去空格
    NSString *OWNER_MOBILE = self.proxyBasicInfoModel.OWNER_MOBILE;
    NSString *DELIVERER_MOBILE = self.proxyBasicInfoModel.DELIVERER_MOBILE;
    self.proxyBasicInfoModel.OWNER_MOBILE = [ICrEAMTool trimWhitespace:OWNER_MOBILE];
    self.proxyBasicInfoModel.DELIVERER_MOBILE = [ICrEAMTool trimWhitespace:DELIVERER_MOBILE];
    
    self.proxyBasicInfoView.ownerPhoneNumberView.contentText = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.ownerPhoneNumberView.contentText];
    self.proxyBasicInfoView.customerNameView.contentText = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.customerNameView.contentText];
    
    if([self.proxyBasicInfoModel.OWNER_PROPERTY isEqualToString:@"11901002"]){
        self.proxyBasicInfoModel.OWNER_NAME = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.ownerNameView.contentText];
        self.proxyBasicInfoModel.DELIVERER = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.customerNameView.contentText];
        
        self.proxyBasicInfoView.ownerNameView.contentText = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.ownerNameView.contentText];
        self.proxyBasicInfoView.customerNameView.contentText = [ICrEAMTool trimWhitespace:self.proxyBasicInfoView.customerNameView.contentText];

        
    }

    if([self.proxyBasicInfoModel.OWNER_PROPERTY isEqualToString:@"11901002"]){
        if([ICrEAMTool isVaildRealName:self.proxyBasicInfoView.ownerNameView.contentText] == NO ){
        
            [self.view showString:@"车主姓名有误，请修改后保存！" forSeconds:1.5];
            return NO;
        }
    }
    if([self.proxyBasicInfoModel.OWNER_PROPERTY isEqualToString:@"11901001"]){
        if([ICrEAMTool isVaildCompanyName:self.proxyBasicInfoView.ownerNameView.contentText] == NO ){
            
            [self.view showString:@"公司名称有误，请修改后保存！" forSeconds:1.5];
            return NO;
        }
    }
    if([ICrEAMTool validateMobile:self.proxyBasicInfoView.ownerPhoneNumberView.contentText] == NO){
        
        [self.view showString:@"车主手机号有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }
    if([ICrEAMTool isVaildRealName:self.proxyBasicInfoView.customerNameView.contentText] == NO ){
        
        [self.view showString:@"联系人姓名有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }
    if([ICrEAMTool validateMobile:self.proxyBasicInfoView.customerPhoneNumberView.contentText] == NO){
        
        [self.view showString:@"联系人手机号有误，请修改后保存！" forSeconds:1.5];
        return NO;
    }

   //----------------------------------------//
    
    
    if (IsStrEmpty(self.proxyOrderInfoView.breakdownCheckReportView.contentText)) {
        [self.view showString:@"故障检查报告为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyOrderInfoView.breakdownDescriptionView.contentText)) {
        [self.view showString:@"用户故障描述为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyBasicInfoModel.IN_MILEAGE)) {
        [self.view showString:@"当前行驶里程为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if (![ICrEAMTool ismileageText:self.proxyBasicInfoModel.IN_MILEAGE]) {
        [self.view showString:@"当前行驶里程有误，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyBasicInfoModel.DELIVERER)) {
        [self.view showString:@"送修人姓名为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyBasicInfoModel.NEXT_MAINTAIN_DATE)) {
        [self.view showString:@"下次保养日期，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyBasicInfoModel.NEXT_MAINTAIN_MILEAGE)) {
        [self.view showString:@"下次保养里程，请检查！" forSeconds:1.5];
        return NO;
    }
    CGFloat next=[self.proxyBasicInfoModel.NEXT_MAINTAIN_MILEAGE doubleValue]-[self.proxyBasicInfoModel.IN_MILEAGE doubleValue];
    if (next<0||next==0) {
        [self.view showString:@"下次保养里程必须大于当前行驶里程，请检查！" forSeconds:1.5];
        return NO;
    }

    if (IsStrEmpty(self.proxyBasicInfoModel.END_TIME_SUPPOSED)) {
        [self.view showString:@"预计交车时间为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if (IsStrEmpty(self.proxyBasicInfoModel.OLD_PART_PROCESS_DESC)) {
        [self.view showString:@"旧件处理为空，请检查！" forSeconds:1.5];
        return NO;
    }
    if([self.proxyBasicInfoModel.CAN_EDIT_WORKSHOP isEqualToString:@"12781001"])
    {
        if (IsStrEmpty(self.proxyBasicInfoModel.IS_EWS_SUCCESS_DESC)) {
            [self.view showString:@"车间标识为空，请检查！" forSeconds:1.5];
            return NO;
        }
    }
    if (IsArrEmpty(self.selectedRepairTypes)) {
        [self.view showString:@"维修类型为空，请检查！" forSeconds:1.5];
        return NO;
    }
    //校验预计交车时间是否小于当前时间
    NSTimeInterval intervel=[[NSDate date] timeIntervalSince1970];
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    NSTimeZone *timeZone = [NSTimeZone localTimeZone];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:@"YYYY-MM-dd HH:mm:ss"];
    
    NSDate *dateTime = [formatter dateFromString:self.proxyBasicInfoModel.END_TIME_SUPPOSED];
    NSTimeInterval intervel2 =  [dateTime timeIntervalSince1970];
    if(intervel2 < intervel)
    {
        [self.view showString:@"预计交车时间小于当前时间，请检查！" forSeconds:1.5];
        return NO;
    }
    
    //上次里程
    CGFloat mileage = [self.proxyBasicInfoModel.IN_MILEAGE floatValue]-[self.proxyBasicInfoModel.LAST_MAINTAIN_MILEAGE floatValue];
    
    if(mileage<0) {
        
        [self.view showString:@"当前进站里程大于等于上次进站里程，请检查！" forSeconds:1.5f];
        
        return NO;
        
    }
    if([self.proxyBasicInfoModel.RO_STATUS isEqualToString:ICMEnumIntToString(ICMEnumProxyTypeNew)]){
        return YES;
    
    }else
    //判定 是否点击过 大众一家
    if([ICrEAMTool isVW])
    {
        
        if (self.haveSyncmember) {
            
        if(!self.isClickedMemberInfo)
        {
            [self.view showString:@"未点击同步会员信息，请点击！" forSeconds:1.5];
            return NO;
        }
    }

    }

    
    

//    else if([self.carModel.lastMillions integerValue] == 0 && mileage == 0){
//        
//        [self.view showString:@"当前进站里程与上次进站里程相同，请检查！" forSeconds:1.5f];
//        return NO;
//        
//    }

    
    return YES;
    
    
}

#pragma mark 如果维修类型为质量担保保养，则有必填字段
- (BOOL)checkRepairType {
    BOOL hasServiceActivity = NO;
    for (int i = 0; i < self.selectedRepairTypes.count; i++) {
        NSString *temp = self.selectedRepairTypes[i];
        if([temp isEqualToString:@"质量担保保养"])
        {
            hasServiceActivity = YES;
        }
    }
    if (hasServiceActivity) {
        if (IsStrEmpty(self.proxyOrderInfoView.proofTypeView.contentText)) {
            [self.view showString:@"凭证类型为空，请检查！" forSeconds:1.5];
            return NO;
        }
        if (IsStrEmpty(self.proxyOrderInfoView.checkCodeView.contentText)) {
            [self.view showString:@"校验码为空，请检查！" forSeconds:1.5];
            return NO;
        }
        
    }
    return YES;
}

#pragma mark 验证维修类型里面是否有项目
- (BOOL)didExistsLabours
{
    BOOL isExist = YES;
    for(int i = 0;i<self.proxyMaintenanceAndPartsView.repairTypes.count;i++)
    {
        NSString *nameStr = self.proxyMaintenanceAndPartsView.repairTypes[i];
        BOOL exist = NO;
        ICMRepairMaintenancesModel *tempModel = [[ICMRepairMaintenancesModel alloc] init];
        [tempModel setupRepairMaintenancesModelWithRoPartsViewWithoutDelItems:self.proxyMaintenanceAndPartsView];
        NSArray *array = tempModel.TT_RO_LABOUR;
        for(ICMRepairMaintenanceModel *model in array)
        {
            if([nameStr isEqualToString:model.REPAIR_TYPE_NAME])
            {
                exist = YES;
                break;
            }
        }
        if(!exist)
        {
            [self.view showString:[NSString stringWithFormat:@"没有%@的项目!",nameStr] forSeconds:1.5f];
            isExist = NO;
            break;
        }
    }
    return isExist;
    
}

#pragma mark -- 车主车辆信息回调
#pragma mark 点击监控信息按钮
- (void)clickMonitorBtn{
    if (self.proxyBasicInfoModel.VIN) {
        [self MonitorViewController];
    }
}

#pragma mark 点击车辆信息
- (void)clickCarInfoBtn{
    if (self.proxyBasicInfoModel.VIN && self.proxyBasicInfoModel.OWNER_NO) {
        [self getInfoWithOWNER_NO:self.proxyBasicInfoModel.OWNER_NO orVin:self.proxyBasicInfoModel.VIN type:@"1"];
    }
}

#pragma mark 点击车主信息
- (void)clickCarOwnerBtn{
    if (self.proxyBasicInfoModel.VIN && self.proxyBasicInfoModel.OWNER_NO) {
        [self getInfoWithOWNER_NO:self.proxyBasicInfoModel.OWNER_NO orVin:self.proxyBasicInfoModel.VIN type:@"0"];
    }
}

#pragma mark 点击会员获取
- (void)clickMemberBtn{
    if (self.proxyBasicInfoModel.VIN) {
        self.isClickedMemberInfo = YES;
        if(![ICrEAMTool isVW])
        {
            self.proxyBasicInfoModel.DX_MEMBER_NO = self.proxyBasicInfoView.VWCardView.contentText;
        }
        [ICrEAMTool isVW]?[self memberInfo]:[self memberSkdWithCardNO:self.proxyBasicInfoModel.DX_MEMBER_NO];
        
    }
}


#pragma mark -- 车主/车辆信息接口
- (void)getInfoWithOWNER_NO:(NSString *)OWNER_NO orVin:(NSString *)vin type:(NSString *)type{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:OWNER_NO forKey:@"OWNER_NO"];
    [parameters setObject:vin forKey:@"VIN"];
    [manager startGetRequestWithURL:QueryOwnerDetail parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        self.carModel = [ICMCarModel yy_modelWithDictionary:data[@"TM_VEHICLE"][0]];
        self.personerModel = [ICMPersonerModel yy_modelWithDictionary:data[@"TM_OWNER"][0]];
        ICMCustomerAndCarViewController *customerAndCar = [ICMCustomerAndCarViewController new];
        customerAndCar.carModel = self.carModel;
        customerAndCar.personerModel = self.personerModel;
        customerAndCar.entrance = [type integerValue];
        [self.navigationController pushViewController:customerAndCar animated:YES];
    }];
}

#pragma mark 调用获取会员信息接口 vw
- (void)memberInfo{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:self.proxyBasicInfoModel.VIN forKey:@"VIN"];
    [manager startGetRequestWithURL:IcreamPoint parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if (IsArr(data[@"TT_HQ_MEMBER"])) {
                if(data[@"TT_HQ_MEMBER"][0])
                {
                    ICMProxyMemberModel *model = [ICMProxyMemberModel yy_modelWithDictionary:data[@"TT_HQ_MEMBER"][0]];
                    if(!model.CARD_NO || [model.CARD_NO isEqualToString:@""])
                    {
                        [self.view showString:@"根据VIN码找不到会员卡信息！" forSeconds:1.5];
                        return;
                    }
                    
                    ICMMemberView *MV = [[ICMMemberView alloc]initWithFrame:CGRectMake(ScreenWidth-ActualWidth(600)-20, ScreenHeight-ActualHeight(400)-100, ActualWidth(600), ActualHeight(400))];
                    CornerRadius(MV, RadiusValue);
                    BorderWidth(MV, 1.0);
                    BorderColor(MV, BorderColor_Gray_Hex);
                    MV.memberCard.contentText = model.CARD_NO;
                    MV.memberName.contentText = model.CARD_NAME;
                    MV.memberStatus.contentText = model.CARD_STATUS;
                    MV.memberType.contentText = model.CARD_TYPE;
                    MV.totalPoint.contentText = model.TOTAL_POINT;
                    MV.availablePoint.contentText = model.AVAILABLE_POINT;
                    MV.frozenPoint.contentText = model.FROZEN_POINT;
                    self.memberView = MV;
                    [Window addSubview:self.blankView];
                    [Window addSubview:self.memberView];
                    //同步到basic view
                    self.proxyBasicInfoView.VWCardView.contentText = model.CARD_NO;
                }
                else
                {
                    //                    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth-ActualWidth(600)-20, ScreenHeight-ActualHeight(400)-100, ActualWidth(600), ActualHeight(400))];
                    //                    CornerRadius(view, RadiusValue);
                    //                    BorderWidth(view, 1.0);
                    //                    BorderColor(view, BorderColor_Gray_Hex);
                    //
                    //                    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(10), ActualHeight(10), ActualWidth(580), ActualHeight(400))];
                    //                    label.text = @"同步信息错误";
                    //                    [view addSubview:label];
                    //                    [Window addSubview:self.blankView];
                    //                    [self.blankView addSubview:self.view];
                    [self.view showString:@"根据VIN码找不到会员卡信息！" forSeconds:1.5];
                }
                
            }
        }
        else
        {
            //            UIView *view = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth-ActualWidth(600)-20, ScreenHeight-ActualHeight(400)-100, ActualWidth(600), ActualHeight(400))];
            //            CornerRadius(view, RadiusValue);
            //            BorderWidth(view, 1.0);
            //            BorderColor(view, BorderColor_Gray_Hex);
            //
            //            UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(ActualWidth(10), ActualHeight(10), ActualWidth(580), ActualHeight(400))];
            //            label.text = @"同步信息错误";
            //            [view addSubview:label];
            //            [Window addSubview:self.blankView];
            //            [self.blankView addSubview:self.view];
            [self.view showString:@"根据VIN码找不到会员卡信息！" forSeconds:1.5];
        }
    }];
}

#pragma mark 获取会员信息接口 sk
- (void)memberSkdWithCardNO:(NSString *)cardNO{
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    //[parameters setObject:self.proxyBasicInfoModel.VIN forKey:@"VIN"];
    if(!cardNO)
    {
        [parameters setObject:@"" forKey:@"CARD_NO"];
    }
    else
    {
        [parameters setObject:cardNO forKey:@"CARD_NO"];
    }
    [manager startGetRequestWithURL:MemberSkd parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            if (IsArr(data[@"TT_MEMBER_SKD"])) {
                ICMProxyMemberSKDModel *model = [ICMProxyMemberSKDModel yy_modelWithDictionary:data[@"TT_MEMBER_SKD"][0]];
                if(!model.CARD_NO || [model.CARD_NO isEqualToString:@""])
                {
                    [self.view showString:@"根据会员卡号找不到会员信息！" forSeconds:1.5];
                    return;
                }
                ICMMemberView *MV = [[ICMMemberView alloc]initWithFrame:CGRectMake((ScreenWidth-ActualWidth(600))/2, (ScreenHeight-ActualHeight(400))/2, ActualWidth(600), ActualHeight(400))];
                CornerRadius(MV, RadiusValue);
                BorderWidth(MV, 1.0);
                BorderColor(MV, BorderColor_Gray_Hex);
                MV.memberCard.contentText = model.CARD_NO;
                MV.memberName.contentText = model.CARD_OWNER;
                MV.memberStatus.contentText = model.SST_CODE;
                MV.memberType.contentText = model.SST_NAME;
                MV.totalPoint.contentText = model.HQ_SCORE;
                MV.availablePoint.contentText = model.IS_USE;
                MV.frozenPoint.contentText = model.SST_SCORE;
                MV.isBinding.contentText = model.IS_BIND;
                self.memberView = MV;
                [Window addSubview:self.blankView];
                [Window addSubview:self.memberView];
            }
            else
            {
                [self.view showString:@"根据会员卡号找不到会员信息！" forSeconds:1.5];
            }
        }
        else
        {
            [self.view showString:@"根据会员卡号找不到会员信息！" forSeconds:1.5];
        }
    }];
}


- (RepairTypeSelectView *)repairTypeSelectView {
    if (!_repairTypeSelectView) {
        _repairTypeSelectView = [RepairTypeSelectView creatSelectViewOtherWithFrame:CGRectMake(ActualWidth(207), ActualHeight(80), ActualWidth(610), ActualHeight(530)) andSelectedData:self.selectedRepairTypes];
    }
    _repairTypeSelectView.delegate = self;
    return _repairTypeSelectView;
}

#pragma mark 维修类型变化回调方法
- (void)didSelectRepairType:(NSNotification *)notification {
    //值变化
    self.hasViewValueChanged = YES;
    if (self.selectedRepairTypes.count) {
        NSMutableString *temp = [[NSMutableString alloc] initWithString:self.selectedRepairTypes[0]];
        for (int i = 1; i < self.selectedRepairTypes.count; i++) {
            [temp appendString:@","];
            [temp appendString:self.selectedRepairTypes[i]];
        }
        self.proxyOrderInfoView.repairTypeView.contentText = temp;
    } else {
        self.proxyOrderInfoView.repairTypeView.contentText = @"";
    }
    [self.proxyMaintenanceAndPartsView resetRepairTypeButton];
    
    //判断是否有 质量担保保养 如果存在 活动页面不可点击
    BOOL hasServiceActivity = NO;
    for (int i = 0; i < self.selectedRepairTypes.count; i++) {
        NSString *temp = self.selectedRepairTypes[i];
        if([temp isEqualToString:@"质量担保保养"])
        {
            hasServiceActivity = YES;
        }
    }
    if(hasServiceActivity)
    {
        //存在 质量担保保养
        if(self.actMsgBgView)
        {
            [self.actMsgBgView removeFromSuperview];
        }
        [self.selecteActivityView addSubview:self.actMsgBgView];
        if(self.actMsgLabel)
        {
            [self.actMsgLabel removeFromSuperview];
        }
        [self.actMsgBgView addSubview:self.actMsgLabel];
    }
    else
    {
        //不存在 质量担保保养
        [self.actMsgBgView removeFromSuperview];
        [self.actMsgLabel removeFromSuperview];
        self.actMsgBgView = nil;
        self.actMsgLabel = nil;
    }
}

//RepairTypeDelegate
#pragma mark 维修类型是否可增加回调方法
- (BOOL)canAddItem:(NSString *)itemStr
{
    //判断是否可以添加
    BOOL hasServiceActivity = NO;
    for (int i = 0; i < self.selectedRepairTypes.count; i++) {
        NSString *temp = self.selectedRepairTypes[i];
        if([temp isEqualToString:@"质量担保保养"])
        {
            hasServiceActivity = YES;
        }
    }
    if(hasServiceActivity)
    {
        //存在质量担保保养的情况
        if(![itemStr isEqualToString:@"质量担保保养"])
        {
            //不能添加别的种类
            [self.repairTypeSelectView showString:@"质量担保保养维修类型不能与其它维修类型并存!" forSeconds:2];
            return NO;
        }
    }
    else
    {
        //不存在服务活动的情况
        if([itemStr isEqualToString:@"质量担保保养"] && self.selectedRepairTypes.count > 0)
        {
            //不能添加服务活动
            [self.repairTypeSelectView showString:@"质量担保保养维修类型不能与其它维修类型并存!" forSeconds:2];
            return NO;
        }
    }
    if (![itemStr isEqualToString:@"质量担保保养"]) {
        self.proxyOrderInfoView.proofTypeView.contentTextButton.enabled = NO;
        self.proxyOrderInfoView.checkCodeView.contentTextFeild.enabled = NO;
        self.proxyOrderInfoView.proofTypeView.contentText = @"";
    } else {
        self.proxyOrderInfoView.proofTypeView.contentTextButton.enabled = NO;
        self.proxyOrderInfoView.checkCodeView.contentTextFeild.enabled = YES;
        self.proxyOrderInfoView.proofTypeView.contentText = @"新首保卡";
    }
    return YES;
}

#pragma mark 维修类型是否能删除回调方法
- (BOOL)canRemoveItem:(NSString *)itemStr
{
    [self.repairMaintenancesModel setupRepairMaintenancesModelWithRoPartsView:self.proxyMaintenanceAndPartsView];
    [self.repairPartsArrayModel setupRepairPartsArrayModelWithRoPartsView:self.proxyMaintenanceAndPartsView];
    BOOL existsItems = NO;
    //检查项目
    NSArray *tempArray1 = self.repairMaintenancesModel.TT_RO_LABOUR;
    for(int i = 0;i<tempArray1.count;i++)
    {
        ICMRepairMaintenanceModel *tempModel = tempArray1[i];
        if([tempModel.REPAIR_TYPE_NAME isEqualToString:itemStr] && ![tempModel.UPDATE_STATUS isEqualToString:@"D"])
        {
            existsItems = YES;
        }
    }
    if(existsItems)
    {
        [self.repairTypeSelectView showString:@"存在该维修类型的项目或零件，不能删除!" forSeconds:2];
        return NO;
    }
    //检查零件
    NSArray *tempArray2 = self.repairPartsArrayModel.TT_RO_REPAIR_PART;
    for(int i = 0;i<tempArray2.count;i++)
    {
        ICMRepairPartsModel *tempModel = tempArray2[i];
        if([tempModel.REPAIR_TYPE_NAME isEqualToString:itemStr] && ![tempModel.UPDATE_STATUS isEqualToString:@"D"])
        {
            existsItems = YES;
        }
    }
    if(existsItems)
    {
        [self.repairTypeSelectView showString:@"存在该维修类型的项目或零件，不能删除!" forSeconds:2];
        return NO;
    }
    return YES;
}

#pragma mark 单据信息页面点击维修类型回调方法
//ICMProxyOrderInfoViewDelegate
- (void)clickRepairTypeBtn
{
    if(self.repairTypeSelectView)
    {
        [self.repairTypeSelectView removeFromSuperview];
        self.repairTypeSelectView = nil;
    }
    [self.blankView addSubview:self.repairTypeSelectView];
    [Window addSubview:self.blankView];
}

//ICMProxyMaintenanceAndPartsViewDelegate
- (void)showRepairEventAndSearchViewWithRepairType:(NSString *)repairType
{
    //显示新增项目和零件页面
}

#pragma mark 监控信息
- (void)MonitorViewController{
    MonitorViewController *mvc = [[MonitorViewController alloc] init];
    mvc.vin = self.proxyBasicInfoModel.VIN;
    mvc.RO_NO = self.proxyBasicInfoModel.RO_NO;
//    [self.navigationController pushViewController:mvc animated:YES];
    mvc.modalPresentationStyle=UIModalPresentationFormSheet;
    [self presentViewController:mvc animated:YES completion:nil];
}

- (void)presentVC:(NSNotification *)notification {
    [self presentViewController:[notification object] animated:YES completion:nil];
}

#pragma mark -- 录音功能
- (AVAudioRecorder *)audioRecorder{
    if (!_audioRecorder) {
        //创建录音文件保存路径
        NSURL *url = [NSURL URLWithString:micTempPath];
        //创建录音格式设置
        NSDictionary *setting = [self getAudioSetting];
        
        //创建录音机
        NSError *error = nil;
        _audioRecorder = [[AVAudioRecorder alloc] initWithURL:url settings:setting error:&error];
        _audioRecorder.delegate = self;
        [_audioRecorder peakPowerForChannel:0];
        _audioRecorder.meteringEnabled = YES;//如果要监控声波则必须设置为YES
        if (error) {
            self.bottomBar.recorderLabel.text = @"录 音";
            [self.view showString:@"录音出错" forSeconds:1.5];
            _haveRecord = NO;
            return nil;
        }
    }
    return _audioRecorder;
}



- (void)setAudioAndPlayer{
    [self setAudioSession];
    //    self.couldRecord = [self.audioRecorder prepareToRecord];
}

- (void)setAudioSession {
    
    AVAudioSession *audioSession = [AVAudioSession sharedInstance];
    
    if ([audioSession respondsToSelector:@selector(requestRecordPermission:)]) {
        [audioSession performSelector:@selector(requestRecordPermission:) withObject:^(BOOL granted) {
            if (granted) {
                NSError *error;
                [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:&error];
                if (error) {
                    error = nil;
                }
                [audioSession setActive:YES error:&error];
                self.couldRecord = YES;
            } else {
                self.couldRecord = NO;
            }
        }];
    }
    
}

- (NSDictionary *)getAudioSetting{
    NSMutableDictionary *dicM = [NSMutableDictionary dictionary];
    //设置录音格式
    [dicM setObject:@(kAudioFormatMPEG4AAC) forKey:AVFormatIDKey];
    //设置录音采样率，8000是电话采样率，对于一般录音已经够了
    [dicM setObject:@(8000) forKey:AVSampleRateKey];
    //设置通道,这里采用单声道
    [dicM setObject:@(1) forKey:AVNumberOfChannelsKey];
    //每个采样点位数,分为8、16、24、32
    [dicM setObject:@(8) forKey:AVLinearPCMBitDepthKey];
    //    //是否使用浮点数采样
    //    [dicM setObject:@(YES) forKey:AVLinearPCMIsFloatKey];
    [dicM setObject:@(AVAudioQualityMax) forKey:AVEncoderAudioQualityKey];
    return dicM;
}

- (void)audioRecorderDidFinishRecording:(AVAudioRecorder *)recorder successfully:(BOOL)flag {
        [self uploadMusic];
}

- (void)audioRecorderEncodeErrorDidOccur:(AVAudioRecorder *)recorder error:(NSError * __nullable)error {
}

- (void)clickRecordBtn{
    if (self.couldRecord){
        [self.audioRecorder record];
    }
}

- (void)clickStopBtn{
    
    [self.audioRecorder stop];
    
}

- (void)clickPauseBtn{
    if (self.couldRecord){
        [self.audioRecorder pause];
    }
}

#pragma 保存成功后重新设置view
- (void)resetViewsAfterSave
{
    if(self.proxyBasicInfoModel.RO_NO && ![self.proxyBasicInfoModel.RO_NO isEqualToString:@""])
    {
        [self didClickCellWithRO_NO:self.proxyBasicInfoModel.RO_NO type:0];
    }
}

#pragma mark 重新获取数据
//0跳委托书 1跳预览
- (void)didClickCellWithRO_NO:(NSString *)RO_NO type:(NSString *)type{
    NSMutableArray *activityModels = [[NSMutableArray alloc] init];
    NSMutableArray *activityTempModels = [[NSMutableArray alloc] init];
    [activityModels removeAllObjects];
    [activityTempModels removeAllObjects];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    [parameters setObject:RO_NO forKey:@"RO_NO"];
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    [manager startGetRequestWithURL:RepairOrderDetils parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        ICMProxyBasicInfoModel *basicModel = [[ICMProxyBasicInfoModel alloc]init];
        if (IsArr(data[@"TT_REPAIR_ORDER"])) {
            basicModel = [ICMProxyBasicInfoModel yy_modelWithDictionary:data[@"TT_REPAIR_ORDER"][0]];
        }
        ICMRepairMaintenancesModel *repairMaintenancesModel = [ICMRepairMaintenancesModel yy_modelWithDictionary:data];
        [repairMaintenancesModel createRepairMaintenanceRoModel];
        ICMRepairPartsArrayModel *repairPartsArrayModel = [ICMRepairPartsArrayModel yy_modelWithDictionary:data];
        [repairPartsArrayModel createRepairPartsRoModel];
        self.lasttMileage=basicModel.IN_MILEAGE;
        NSArray *dataArray = data[@"TT_ACTIVITY"];
        __block int countIndex = 0;
        if(dataArray && dataArray.count > 0)
        {
            if (dataArray.count != 0 && dataArray) {
                
                for (NSDictionary *activityData in dataArray) {
                    ICMProxyActivityViewModel *model = [[ICMProxyActivityViewModel alloc]init];
                    model.title = activityData[@"ACTIVITY_NAME"];
                    model.type = activityData[@"ACTIVITY_TYPE"];
                    model.subTitle = activityData[@"ACTIVITY_CODE"];
                    model.startTime = [ICrEAMTool getDateWithTimeInterval:activityData[@"SALES_DATE_BEGIN"]];
                    model.endTime = [ICrEAMTool getDateWithTimeInterval:activityData[@"SALES_DATE_END"]];
                    //ischecked 判断
                    if(activityData[@"IS_CHECKED"])
                    {
                        model.IS_CHECKED = activityData[@"IS_CHECKED"];
                    }
                    else
                    {
                        model.IS_CHECKED = ICMEnumIntToString(ICMEnumYESOrNONO);
                    }
                    [activityModels addObject:model];
                }
                for (ICMProxyActivityViewModel *viewModel in activityModels) {
                    
                    ICMProxyActivityViewModel *temp = [[ICMProxyActivityViewModel alloc]init];
                    temp.title = viewModel.title;
                    temp.type = viewModel.type;
                    temp.subTitle = viewModel.subTitle;
                    temp.startTime = viewModel.startTime;
                    temp.endTime = viewModel.endTime;
                    temp.IS_CHECKED = viewModel.IS_CHECKED;
                    [activityTempModels addObject:temp];
                    
                    if(viewModel.subTitle)
                    {
                        //去除首位空格和换行
                        viewModel.subTitle = [viewModel.subTitle stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
                        NSString *url = [NSString stringWithFormat:@"/INFO_ICREAM_WEB/App/queryActivityDetail?ACTIVITY_CODE=%@",viewModel.subTitle];
                        [[[ICMRequestManager alloc] init]startGetRequestWithURL:url parameters:nil inHudView:self.view andResultBlock:^(id data, NSString *failText) {
                            countIndex++;
                            if (!failText) {
                                NSArray *labelArray = data[@"TT_ACTIVITY_LABOUR"];
                                NSArray *partsArray = data[@"TT_ACTIVITY_PART"];
                                for (NSDictionary *labourDic in labelArray) {
                                    ICMProxyActivityLabourModel *labelModel = [ICMProxyActivityLabourModel yy_modelWithJSON:labourDic];
                                    [viewModel.allActivityModel.TT_ACTIVITY_LABOUR addObject:labelModel];
                                    
                                    ICMRepairMaintenanceModel *labourTempModel = [ICMRepairMaintenanceModel yy_modelWithJSON:labourDic];
                                    labourTempModel.ACTIVITY_NO = labelModel.ACTIVITY_CODE;
                                    [temp.allActivityModel.TT_ACTIVITY_LABOUR addObject:labourTempModel];
                                }
                                for (NSDictionary *partDic in partsArray) {
                                    ICMProxyActivityPartsModel *partsModel = [ICMProxyActivityPartsModel yy_modelWithJSON:partDic];
                                    [viewModel.allActivityModel.TT_ACTIVITY_PART addObject:partsModel];
                                    
                                    ICMRepairPartsModel *partsTempModel = [ICMRepairPartsModel yy_modelWithJSON:partDic];
                                    partsTempModel.ACTIVITY_NO = partsModel.ACTIVITY_CODE;
                                    //给零件数量扶植
                                    partsTempModel.OBLIGATED_QUANTITY = partsModel.PART_QUANTITY;
                                    //单价
                                    partsTempModel.SALES_PRICE = partsTempModel.PART_SALES_PRICE;
                                    //零件销售价格
                                    float tempCount = [partsTempModel.OBLIGATED_QUANTITY floatValue];
                                    float tempPrice = [partsTempModel.SALES_PRICE floatValue];
                                    partsTempModel.PART_SALES_AMOUNT = [NSString stringWithFormat:@"%f",tempCount * tempPrice];
                                    [temp.allActivityModel.TT_ACTIVITY_PART addObject:partsTempModel];
                                }
                                
                            }else{
                                
                            }
                            if(countIndex == dataArray.count)
                            {
                                //跳转
                                if ([type integerValue] == 0)
                                {
                                    self.repairMaintenancesModel = repairMaintenancesModel;
                                    self.proxyBasicInfoModel =basicModel;
                                    self.repairPartsArrayModel = repairPartsArrayModel;
                                    self.activityModels = activityModels;
                                    self.activityTempModels = activityTempModels;
                                    [self setupBigScrollView];
                                    self.bigScrollView.contentOffset = CGPointMake(self.headerView.selectedIndex * ScreenWidth, 0);
                                }
                            }
                        }];
                    }
                    
                }
            }
        }
        else
        {
            //跳转
            if ([type integerValue] == 0)
            {
                self.repairMaintenancesModel = repairMaintenancesModel;
                self.proxyBasicInfoModel =basicModel;
                self.repairPartsArrayModel = repairPartsArrayModel;
                self.activityModels = activityModels;
                self.activityTempModels = activityTempModels;
                [self setupBigScrollView];
                self.bigScrollView.contentOffset = CGPointMake(self.headerView.selectedIndex * ScreenWidth, 0);
            }
        }
        
        
    }];
}

- (NSMutableArray *)selectedActivityModels
{
    if(!_selectedActivityModels)
    {
        _selectedActivityModels = [[NSMutableArray alloc] init];
    }
    return _selectedActivityModels;
}

#pragma mark 各页面值变化通知
- (void)viewValueChanged:(NSNotification *)notification
{
    self.hasViewValueChanged = YES;
}

- (void)setHasViewValueChanged:(BOOL)hasViewValueChanged
{
    _hasViewValueChanged = hasViewValueChanged;
    super.super_hasViewValueChanged = _hasViewValueChanged;
}

#pragma mark    返回按钮
- (void)backButtonClick
{
    if(self.hasViewValueChanged)
    {
        UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"提示" message:@"是否放弃当前数据？" delegate:self cancelButtonTitle:@"否" otherButtonTitles:@"是", nil];
        alert.tag = 101;
        [alert show];
    }
    else
    {
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex NS_DEPRECATED_IOS(2_0, 9_0)
{
    if(alertView.tag == 101)
    {
        if(buttonIndex == 0)
        {
            //否
        }
        else
        {
            //是 放弃
            [self.navigationController popViewControllerAnimated:YES];
        }
    }
}
-(void)getLABOUR_PRICE:(NSString *)carModel{
    
    ICMRequestManager *manager = [[ICMRequestManager alloc]init];
    NSMutableDictionary *parameters = [NSMutableDictionary dictionary];
    
    if(IsStrEmpty(carModel)){
        carModel = @"";
    }
    
    [parameters setObject:carModel forKey:@"MODEL_CODE"];
    [manager startGetRequestWithURL:queryLabourPrice parameters:parameters inHudView:self.view andResultBlock:^(id data, NSString *failText) {
        if ([data[@"CODE"] integerValue] == 200) {
            
            LABOUR_PRICE = data[@"LABOUR_PRICE"];
            
            self.proxyMaintenanceAndPartsView.repairTypes = self.selectedRepairTypes;
            self.proxyMaintenanceAndPartsView.delegate = self;
            self.proxyMaintenanceAndPartsView.modelCode = carModel;
            self.proxyMaintenanceAndPartsView.SERIES_LABOUR_PRICE = LABOUR_PRICE;
            self.proxyBasicInfoModel.LABOUR_PRICE = LABOUR_PRICE;
            [self.proxyMaintenanceAndPartsView setupPartsViewWithBasicModel:self.proxyBasicInfoModel];
           
        }
        
    }];
    
}


@end
